<template>
  <div class="app-container">
    <!-- 条件筛选组件 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="用户名:">
        <el-input v-model.trim="query.username"></el-input>
      </el-form-item>
      <el-form-item label="手机:">
        <el-input v-model.trim="query.mobile"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="openAdd"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 显示列表   -->
    <el-table :data="list" stripe border style="width: 100%">
      <el-table-column align="center" label="序号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" prop="username" label="用户名">
      </el-table-column>
      <el-table-column align="center" prop="nickName" label="昵称">
      </el-table-column>
      <el-table-column align="center" prop="mobile" label="手机号">
      </el-table-column>
      <el-table-column align="center" prop="email" label="邮箱">
      </el-table-column>
      <el-table-column
        align="center"
        prop="isAccountNonExpired"
        label="账号过期"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAccountNonExpired === 0" type="danger"
            >已过期</el-tag
          >
          <el-tag v-if="scope.row.isAccountNonExpired === 1" type="success"
            >未过期</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="isAccountNonLocked"
        label="账号锁定"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAccountNonLocked === 0" type="danger"
            >锁定</el-tag
          >
          <el-tag v-if="scope.row.isAccountNonLocked === 1" type="success"
            >正常</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="isCredentialsNonExpired"
        label="密码过期"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isCredentialsNonExpired === 0" type="danger"
            >已过期</el-tag
          >
          <el-tag v-if="scope.row.isCredentialsNonExpired === 1" type="success"
            >未过期</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column align="center" prop="isEnabled" label="是否可用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isEnabled === 0" type="danger">删除</el-tag>
          <el-tag v-if="scope.row.isEnabled === 1" type="success">可用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="330px">
        <template slot-scope="scope" v-if="scope.row.isEnabled == 1">
          <el-button
            type="success"
            size="mini"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="handleRole(scope.row.id)"
            >设置角色</el-button
          >
          <el-button type="primary" size="mini" @click="handlePwd(scope.row.id)"
            >密码修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>

    <edit
      :title="edit.title"
      :formData="edit.formData"
      :visible="edit.visible"
      :remoteClose="remoteClose"
    />

    <el-dialog title="设置角色" :visible.sync="role.visible" width="65%">
      <!-- roleids是用户当前所拥有的角色id，saveUserRole是子组件进行触发提交选择的角色id -->
      <role :roleIds="role.roleIds" @saveUserRole="saveUserRole" />
    </el-dialog>

    <password
      title="修改密码"
      :userId="pwd.userId"
      :visible="pwd.visible"
      :remoteClose="remotePwdClose"
    />
  </div>
</template>

<script>
import * as api from "@/api/user";
import Edit from "./edit";
import Role from "../role";
import Password from "./password";
export default {
  data() {
    return {
      list: [], //列表数据
      page: {
        current: 1,
        size: 20,
        total: 0, //总记录数
      },
      query: {},
      edit: {
        title: "",
        formData: {},
        visible: false,
      },
      role: {
        //设置角色弹出组件
        visible: false,
        roleIds: [],
        userId: null, //保存用户角色时需要使用
      },
      pwd: {
        userId: null,
        visible: false,
      },
    };
  },
  created() {
    this.fetchData();
  },
  components: { Edit, Role, Password },

  methods: {
    fetchData() {
      api.getList(this.query, this.page.current, this.page.size).then((res) => {
        this.list = res.data.records;
        this.page.total = res.data.total;
      });
    },
    // val是指每页显示多少条
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    // 当页码改变后触发
    handleCurrentChange(val) {
      this.page.current = val;
      this.fetchData();
    },
    // 查询
    queryData() {
      // 页码设置为1
      this.page.current = 1;
      this.fetchData();
    },
    // 重载
    reload() {
      this.query = {};
      this.fetchData();
    },
    handleEdit(id) {
      api.getById(id).then((res) => {
        if ((res.code = 20000)) {
          this.edit.formData = res.data;
          this.edit.title = "编辑";
          this.edit.visible = true;
        }
      });
    },
    handleDelete(id) {
      this.$confirm("确认删除这条记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          api.deleteById(id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
          });
          this.fetchData();
        })
        .catch(() => {});
    },
    //设置角色
    handleRole(id) {
      this.role.userId = id;
      api.getRoleIdsByUserId(id).then((res) => {
        this.role.roleIds = res.data;
        this.role.visible = true;
      });
    },

    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },
    openAdd() {
      this.edit.title = "新增—————默认密码与用户名一致";
      this.edit.visible = true;
    },
    saveUserRole(ids) {
      api.saveUserRole(this.role.userId, ids).then((res) => {
        if ((res.code = 20000)) {
          this.$message({ message: "角色分配成功", type: "success" });
          this.role.visible = false;
        } else {
          this.$message({ message: "角色分配失败", type: "error" });
        }
      });
    },
    // 关闭密码窗口
    remotePwdClose() {
      this.pwd.userId = null;
      this.pwd.visible = false;
      this.fetchData();
    },
    //修改密码
    handlePwd(id) {
      this.pwd.userId = id;
      this.pwd.visible = true;
    },
  },
};
</script>

<style scoped>
</style>